home *** CD-ROM | disk | FTP | other *** search
- #
- # This file is part of OpenVIP (http://openvip.sourceforge.net)
- #
- # Copyright (C) 2002-2003
- # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
- #
- # This program is licensed under GNU General Public License version 2;
- # see file COPYING in the top level directory for details.
- #
- # $Id: conv.py,v 1.15 2004/01/10 12:21:20 vaclavslavik Exp $
- #
-
- import xml.dom.minidom
- import model
- import string
- import openvip
- import globals
-
- class ParsingError(Exception):
- def __init__(self, msg):
- self.msg = msg
- def __str__(self):
- return self.msg
-
- DOCTYPE_PUBLIC_ID = "-//OPENVIP//DTD Timeline Format V1.0//EN"
- DOCTYPE_SYSTEM_ID = "http://openvip.sourceforge.net/dtds/openvip-timeline.dtd"
- NEAR = 0.1
- #main functions
-
- def dom_to_data(dom):
- "creates fills and returns model.Timeline object from given dom"
- dom.normalize()
- data = model.Timeline()
- if dom.firstChild.nodeName == "timeline":
- try:
- storeNetwork(dom.getElementsByTagName("timeline")[0],data)
- except KeyError, e:
- raise ParsingError("%s"% e)
- checkInput(data)
- return data
-
-
- def data_to_dom(data,xmlns,version):
- "Extracts data to dom object"
- #dom = xml.dom.minidom.Document()
- import conv2
- dom = conv2.myDocument()
-
- doctype = xml.dom.minidom.DocumentType("timeline")
- doctype.publicId = DOCTYPE_PUBLIC_ID
- doctype.systemId = DOCTYPE_SYSTEM_ID
- dom.doctype = doctype
-
- el = dom.createElement("timeline")
- el.setAttribute("version",version)
- el.setAttribute("xmlns",xmlns)
-
- extractObjects(data,el)
- extractTransitions(data,el)
- extractGroups(data,el)
-
- dom.appendChild(el)
-
- return dom
-
- #auxiliary functions
-
-
- def checkDTD(data,e):
- import xmlhelpers
- import tempfile
- import os
-
- fname = tempfile.mktemp("conv2_tmp")
- f=open(fname, 'w')
- dom2 = data_to_dom(data,"http://openvip.sourceforge.net/timeline-format","1.0")
- f.write(dom2.toxml())
- f.close()
- dom2.unlink()
- valid = xmlhelpers.validate(fname)
- os.remove(fname)
- if valid != 1: e.msg = "Document does not match to it's DTD"
- return valid == 1
-
- def checkFiles(data,e):
- import os
- for o in data.objects:
- if not os.path.exists(o.src_spec["filename"]):
- e.msg = o.src_spec["filename"] + " does not exist"
- return False
- return True
-
- def checkObjectLengths(data,e):
- #lib = openvip.Library()
- for o in data.objects:
- if abs((o.time_to - o.time_from) - (o.src_to - o.src_from))>NEAR\
- and o.src_to != 0:
- e.msg = """Object length differs from source specification
- (object.src_from, object.src_to)"""
- return False
- i = globals.get_file_info(o.src_spec["filename"])
- length = o.time_to - o.time_from
-
- if o.track[0] == "V":
- for v in i.video_streams:
- if o.src_from == 0 and o.src_to==0 and\
- abs(length - float(v.length)/v.fps)>NEAR:
- e.msg = "Wrong length of object %s"%o.id
- return False
- if o.src_to > float(v.length)/v.fps:
- e.msg = "Wrong length of object %s"%o.id
- return False
- else :
- for a in i.audio_streams:
- if o.src_from == 0 and o.src_to==0 and\
- abs(length - float(a.length)/a.sample_rate)>NEAR:
- e.msg = "Wrong length of object %s"%o.id
- return False
- if o.src_to > float(a.length)/a.sample_rate:
- e.msg = "Wrong length of object %s"%o.id
- return False
- return True
-
- def checkObjectsConflicts(data,e):
- return True
-
- def checkInput(data):
- e = model.Error("")
- if not checkDTD(data,e):
- raise e
- return False
- if not checkFiles(data,e):
- raise e
- return False
- if not checkObjectLengths(data,e):
- raise e
- return False
- if not checkObjectsConflicts(data,e):
- raise e
- return False
- return True
-
-
- def storeNetwork(node,data):
- for child in node.childNodes:
- if child.nodeName == "object":
- storeObject(child,data)
- elif child.nodeName == "transition_object":
- storeTransitionObject(child,data)
- for child in node.childNodes:
- if child.nodeName == "group":
- storeGroup(child,data)
-
- def storePosition(node,object):
- "If current Position attributes exist they are stored into object"
- a = node.attributes._attrs
- object.time_from = string.atof(node.attributes["time_from"].value)
- object.time_to = string.atof(node.attributes["time_to"].value)
- object.track = node.attributes["track"].value
- if a.has_key("direction"):
- object.direction = node.attributes["direction"].value
-
- def storeParams(node,dir):
- for child in node.childNodes:
- if child.nodeName == "param":
- dir[child.attributes["name"].value] = \
- child.firstChild.nodeValue.strip()
-
- def storeSource(node,object):
- object.src_from = string.atof(node.attributes["src_from"].value)
- object.src_to = string.atof(node.attributes["src_to"].value)
- for child in node.childNodes:
- if child.nodeName == "channel":
- object.src_channel = child.attributes["name"].value
- if child.nodeName == "params":
- storeParams(child,object.src_spec)
-
- def storeFilters(node,object):
- for child in node.childNodes:
- if child.nodeName == "filter":
- filter = model.Filter()
- a = child.attributes._attrs
- filter.name = child.attributes["name"].value
- filter.active = True
-
- if a.has_key("active") and\
- string.lower(child.attributes["active"].value)=="false":
- filter.active = False
- for child2 in child.childNodes:
- if child2.nodeName == "params":
- storeParams(child2,filter.params)
- object.filters.append(filter)
-
- def storeObject(node,data):
- object = model.Object()
- object.id = node.attributes["id"].value
- for child in node.childNodes:
- if child.nodeName == "position":
- storePosition(child,object)
- if child.nodeName == "source":
- storeSource(child,object)
- if child.nodeName == "filters":
- storeFilters(child,object)
-
- data.objects.append(object)
-
- def storeTransitionObject(node,data):
- trans = model.Transition()
- trans.id = node.attributes["id"].value
- for child in node.childNodes:
- if child.nodeName == "transition":
- trans.name = child.attributes["name"].value
- for child2 in child.childNodes:
- if child2.nodeName == "params":
- storeParams(child2,trans.params)
- if child.nodeName == "position":
- storePosition(child,trans)
-
- data.transitions.append(trans)
-
- def storeGroup(node,data):
- group = model.Group()
- group.id = node.attributes["id"].value
- for child in node.childNodes:
- if child.nodeName == "item":
- o_ref = None
- for o in data.objects + data.transitions:
- if o.id == child.attributes["ref"].value:
- o_ref = o
- if o_ref == None:
- raise KeyError()
- group.objects.append(o_ref)
- data.groups.append(group)
-
-
- def extractObjects(data,node):
- dom = xml.dom.minidom.Document()
- for object in data.objects:
- el = dom.createElement("object")
- el.setAttribute("id",object.id)
- pos = extractPosition(object)
- el.appendChild(pos)
- src = dom.createElement("source")
- src.setAttribute("src_from",str(object.src_from))
- src.setAttribute("src_to",str(object.src_to))
- channel = dom.createElement("channel")
- channel.setAttribute("name",object.src_channel)
- src.appendChild(channel)
- params = extractParams(object.src_spec)
- src.appendChild(params)
- el.appendChild(src)
- filters = dom.createElement("filters")
- extractFilters(object.filters,filters)
- if 0 < len(filters.childNodes):
- el.appendChild(filters)
- node.appendChild(el)
- dom.unlink()
-
- def extractPosition(item):
- dom = xml.dom.minidom.Document()
- pos = dom.createElement("position")
- pos.setAttribute("time_from",str(item.time_from))
- pos.setAttribute("time_to",str(item.time_to))
- pos.setAttribute("track",item.track)
- try:
- pos.setAttribute("direction",item.direction)
- except AttributeError:
- pass
-
-
- dom.unlink()
- return pos
-
- def extractParams(dict):
- dom = xml.dom.minidom.Document()
- params = dom.createElement("params")
- for item in dict.items():
- param = dom.createElement("param")
- param.setAttribute("name",item[0])
- param.appendChild(dom.createTextNode(item[1]))
- params.appendChild(param)
- return params
- dom.unlink()
-
- def extractFilters(list,filters):
- dom = xml.dom.minidom.Document()
- for item in list:
- filter = dom.createElement("filter")
- filter.setAttribute("name",item.name)
- if item.active:
- filter.setAttribute("active","true")
- else: filter.setAttribute("active","false")
-
- params = extractParams(item.params)
- filter.appendChild(params)
- filters.appendChild(filter)
-
- def extractTransitions(data,node):
- dom = xml.dom.minidom.Document()
- for trans in data.transitions:
- tr = dom.createElement("transition_object")
- tr.setAttribute("id",trans.id)
- pos = extractPosition(trans)
- tr.appendChild(pos)
- tr2 = dom.createElement("transition")
- tr2.setAttribute("name",trans.name)
- params = extractParams(trans.params)
- tr2.appendChild(params)
- tr.appendChild(tr2)
- node.appendChild(tr)
-
- dom.unlink()
-
- def extractGroups(data,node):
- dom = xml.dom.minidom.Document()
- for group in data.groups:
- gr = dom.createElement("group")
- gr.setAttribute("id",group.id)
- for object in group.objects:
- item = dom.createElement("item")
- item.setAttribute("ref",object.id)
- gr.appendChild(item)
- node.appendChild(gr)
- dom.unlink()
-
-
- #test function
- def simple_test(file_name):
- dom1 = xml.dom.minidom.parse(file_name)
- data = dom_to_data(dom1)
- dom1.unlink()
-
- dom2 = data_to_dom(data,"http://openvip.sourceforge.net/timeline-format","1.0")
- f=open('workfile.xml', 'w')
- f.write(dom2.toxml())
- f.close()
- dom2.unlink()
-
- #simple_test("test.xml")
-